package com.yangye.pisystem.service.impl;

import com.yangye.picommon.core.service.impl.BaseServiceImpl;
import com.yangye.picommon.exception.PiException;
import com.yangye.pisystem.dao.SysDictDataMapper;
import com.yangye.pisystem.dao.SysDictMapper;
import com.yangye.pisystem.domain.SysDict;
import com.yangye.pisystem.domain.SysDictData;
import com.yangye.pisystem.service.SysDictService;
import com.yangye.pisystem.vo.SysDictDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* @author yangye
* @date 2020-3-18
* 自动生成代码
*/
@Service
public class SysDictServiceImpl extends BaseServiceImpl<SysDictMapper, SysDict> implements SysDictService {

	@Autowired
	private SysDictDataMapper sysDictDataMapper;

	@Override
	public List<SysDictDataVO> findDictDataByDictName(String dictName) {
		return sysDictDataMapper.selectDictDataByDictName(dictName);
	}

	@Override
	public List<SysDictDataVO> getDictData(Long dictId) {
		return sysDictDataMapper.selectDictDataByDictId(dictId);
	}

	@Override
	public SysDict add(SysDict entity) {
		SysDict byName = this.findByName(entity.getName());
		if (byName != null) {
			throw new PiException("字典名称已存在");
		}
		return super.add(entity);
	}

	@Override
	public SysDict update(SysDict entity) {
		SysDict byName = this.findByName(entity.getName());
		if (byName != null && byName.getId() != entity.getId()) {
			throw new PiException("字典名称已存在");
		}
		return super.update(entity);
	}

	private SysDict findByName(String name) {
		SysDict query = new SysDict();
		query.setName(name);
		return this.dao.selectOne(query);
	}
}
